    //
//  CommentViewController.m
//  SpendFunk
//
//  Created by bm on 4/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CommentViewController.h"
#import "Global.h"
#import "image.h"
#import "SpendFunkAppDelegate.h"
@implementation CommentViewController

-(id)init{
	return self;
}

-(void)loadView{
	[super loadView];
	
	[self initView];
}

-(void)initView{
	NSString *path = nil;
	UIImage *img = nil;
	
	////
	path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, -22, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[self.view addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	//BKGR
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
	[imgvBkgr setImage:img];
	[self.view addSubview:imgvBkgr];
	[imgvBkgr setUserInteractionEnabled:YES];
	[imgvBkgr release];
	
	//bkgr user
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_LOCATION];
	img = [UIImage imageNamed:path];	
	UIImageView *imgvBkgrUser = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
	[imgvBkgrUser setImage:img];
	[imgvBkgrUser setUserInteractionEnabled:YES];
	[self.view addSubview:imgvBkgrUser];
	[imgvBkgrUser release];
	
	//BACK
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];	
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, -14, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	//
	m_pTxtComment = [[UITextView alloc] initWithFrame:CGRectMake(20, 50, 180, 70)];
	[m_pTxtComment setAutocorrectionType:UITextAutocorrectionTypeNo];
	[self.view addSubview:m_pTxtComment];
	[m_pTxtComment release];
	////
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_COMMENT];
	img = [UIImage imageNamed:path];
	
	m_pBtnComment = [[UIButton alloc] initWithFrame:CGRectMake(320/2-img.size.width/2, 175, img.size.width, img.size.height)];
	[self.view addSubview:m_pBtnComment];
	[m_pBtnComment setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnComment addTarget:nil action:@selector(onClickSendComment) forControlEvents:UIControlEventTouchUpInside];
	[m_pBtnComment release];
	// Please wait ... 
	_blockerView = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, 200, 60)] autorelease];
	_blockerView.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.8];
	_blockerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
	_blockerView.alpha = 0.0;
	_blockerView.clipsToBounds = YES;
	if ([_blockerView.layer respondsToSelector: @selector(setCornerRadius:)]) [(id) _blockerView.layer setCornerRadius: 10];
	
	UILabel								*label = [[[UILabel alloc] initWithFrame: CGRectMake(0, 5, _blockerView.bounds.size.width, 15)] autorelease];
	label.text = NSLocalizedString(@"Please Wait…", nil);
	label.backgroundColor = [UIColor clearColor];
	label.textColor = [UIColor whiteColor];
	label.textAlignment = UITextAlignmentCenter;
	label.font = [UIFont boldSystemFontOfSize: 15];
	[_blockerView addSubview: label];
	
	UIActivityIndicatorView				*spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite] autorelease];
	
	spinner.center = CGPointMake(_blockerView.bounds.size.width / 2, _blockerView.bounds.size.height / 2 + 10);
	[_blockerView addSubview: spinner];
	[self.view addSubview: _blockerView];
	[spinner startAnimating];
	
}
-(void)setSpendID:(NSString *)spendID{
	if (m_pSpendID) {
		[m_pSpendID release];
		m_pSpendID = nil;
	}
	m_pSpendID = [spendID retain];
}
- (void)onClickBack{
	[m_pTxtComment resignFirstResponder];
	m_pTxtComment.text = nil;
	[g_AppDelegate openFeedView];
}
-(void)onClickSendComment{
	[m_pTxtComment resignFirstResponder];
	if ([m_pTxtComment.text isEqualToString:@""]) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No text to comment" message:@"Please input text!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	_blockerView.alpha = 1.0;
	NSString *post = [NSString stringWithFormat:@"userid=%@&spend_id=%@&comment_text=%@", g_strLoginUserId, m_pSpendID,m_pTxtComment.text];
	NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
	NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
	NSURL *url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/comment/add_comment"];
	
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
	[theRequest setHTTPMethod:@"POST"];  
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
	[theRequest setHTTPBody:postData];	
	
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	if( theConnection )
	{
		m_pWebData = [[NSMutableData data] retain];
		isContinueParse = true;
	}
	else
	{
		
	}
}


#pragma mark delegate connection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	[m_pWebData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[m_pWebData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot connect to internet" message:@"Please connect to internet!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	
	[alert release];
	[connection release];
	[m_pWebData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSData *loginStatus = [[NSData alloc] initWithBytes: [m_pWebData mutableBytes] length:[m_pWebData length]];
	
	//NSString *a = [[NSString alloc] initWithData:loginStatus encoding:NSUTF8StringEncoding];
	//NSLog(@"%@",a);
	
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:loginStatus];
	[xmlParser setDelegate:self];
	
	BOOL success = [xmlParser parse];
	
	if(success)
	{
		NSLog(@"No Errors");
		
	}
	else
	{
		NSLog(@"Error Error Error!!!");
	}
	
	[connection release];
	[m_pWebData release];
}

#pragma mark delegate xmlParser
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict {
	if(!currentElement) 
		currentElement = [[NSString alloc] initWithString:elementName];
	else
		currentElement = [elementName retain];
	
	NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	NSLog(@"Processing Value: %@", string);
	if([currentElement isEqualToString:@"status"])
	{
		if(!currentElementValue) 
			currentElementValue = [[NSString alloc] initWithString:string];
		else
			currentElementValue = [string retain];
		
		if([currentElementValue isEqualToString:@"false"])
		{
		}
		else
		{
			// commit thanh cong
			_blockerView.alpha =0.0;
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Success commit" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
			[alert show];
			
			[alert release];
		}
	}
	//dang nhap khong thanh cong se hien info
	if([currentElement isEqualToString:@"info"])
	{
		_blockerView.alpha =0.0;
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		
		[alert release];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	
	[currentElement release];
	currentElement = nil;
	[currentElementValue release];
	currentElementValue = nil;
}


#pragma mark Event touch screen
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
	NSLog(@"Touch end");
	[m_pTxtComment resignFirstResponder];
}
#pragma mark textfield delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
	return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
